﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Floating : MonoBehaviour
{
    public float floatRange = 0.3f;
    public float duration = 5f;

    private float timeCount = 0f;
    private Vector3 top;
    private Vector3 bottom;
    private bool isUp = true;

    private Vector3 velocity = new Vector3(0.0f, 0.0f, 0.0f);

    void Start()
    {
        bottom = this.transform.localPosition;
        bottom.y -= floatRange * 0.5f;
        top = new Vector3(bottom.x, bottom.y + floatRange, bottom.z);
    }

    void Update()
    {
        timeCount += Time.deltaTime;
        if (timeCount >= duration)
        {
            timeCount -= duration;
            isUp = !isUp;
        }

        Vector3 curPos;
        if (isUp)
        {
            curPos = Vector3.Lerp(bottom, top, timeCount / duration);
        }
        else
        {
            curPos = Vector3.Lerp(top, bottom, timeCount / duration);            
        }
        this.transform.localPosition = curPos;
    }
}
